--[[
FilePath: \Lua\View\DungeonRandomEventUI\UIRandomEventPK.lua
主线随机事件--玩家PK
--]]

require 'View/UIBase/UIBase'

UIRandomEventPK = UIBase.New(PanelResNames.UIRandomEventPK)
local _M = UIRandomEventPK

_M.hasInit = false

function _M.Init(func)
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("Btn_BgClose"), self.OnBack);
        self:RegisterOnClick(self:GetChild("Btn_Confirm"), self.OnConfirm);

        self.playerHead = self:GetChild("Btn_YourHead");
        self.textPower = self:GetChild("Text_YourPower");
        self.heroPos = self:GetChild("Graph_YourHeroPos");
        self.playerName = self:GetChild("Text_YourName");
        self.playerGuildName = self:GetChild("Text_YourGuildName");

        self.enemyHead = self:GetChild("Btn_EnemyHead");
        self.enemyPower = self:GetChild("Text_EnemyPower");
        self.enemyHeroPos = self:GetChild("Graph_EnemyHeroPos");
        self.enemyName = self:GetChild("Text_EnemyName");
        self.enemyGuildName = self:GetChild("Text_EnemyGuildName");

        self.textPkTips = self:GetChild("Text_PkTips");

        _M.hasInit = true

        self:addEvent();
    end
end

function _M:addEvent()
    self:RegisterEvent(Config.EventType.Fresh_DungeonRandomEventAccept,self.OpenFriendCompeteBattle);
end

function _M:removeEvent()
    self:UnRegisterEvent(Config.EventType.Fresh_DungeonRandomEventAccept,self.OpenFriendCompeteBattle);
end

-- 显示
function _M:OnEnable()
    if not self.hasInit then
        self:OnBack();
        return;
    end
    self:refresh();
end

function _M:refresh()
    self.data = logicMgr.DungeonRandomEventManager.getDungeonRandomEventData();
    if self.data == nil or self.data["foePlayerId"] == nil then
        self:OnBack();
        return;
    end
    self:updateView();
end

function _M:updateView()
    self.isBuy = false;
    self:showMyInfo();
    self:showEnemyInfo();
end

function _M:showMyInfo()
    self:SetPlayerIcon(self.playerHead:GetChild("head"):GetChild("HeadIcon"), logicMgr.HeroManager.GetRoleInfo("pic"), self.playerHead:GetChild("frame"), logicMgr.HeroManager.GetRoleInfo("picFrame"));
    self.playerName.text = logicMgr.HeroManager.GetRoleName();
    local unionName = logicMgr.GuildManager.getUnionName();
    if unionName ~= nil and unionName ~= "" then
        self.playerGuildName.text = unionName;
    else
        self.playerGuildName.text = getLanguage("GuildBattlePlusRankTips");
    end
    self.textPower.text = HelperFunc.FormatResourceNum(logicMgr.HeroManager.GetHerosSumPower(), true);

    local heroID = tostring(logicMgr.SkinManager.getHeroModelIdBySkinId(logicMgr.HeroManager.GetRoleInfo("pic")));
    --local useSkinId = logicMgr.SkinManager.getHeroSkinId(heroID);
    local path = logicMgr.HeroManager.GetHeroDrawingRolePath(heroID);
    resMgr:LoadModelPrefab(path, function(go)
        if self.myRole ~= nil then
            destroyImmediate(self.myRole);
            self.myRole = nil
        end
        self.myRole = newObject(go);
        self.myRole.name = path;
        self.myRole:AddComponent(TCanvas)
        if self.myWrapper == nil then
            self.myWrapper = GoWrapper.New()
        end
        self.myWrapper:SetWrapTarget(self.myRole,false);
        Util.Lua_SetLocalPositionXYZ(self.myRole, 60, -300, 0);
        Util.Lua_SetLocalScaleXYZ(self.myRole, 110, 110, 110);
        self.heroPos:SetNativeObject(self.myWrapper);
        local enemyObj = Util.Lua_FindGameObject(self.myRole, "Animation");
        local shadow = Util.Lua_FindGameObject(self.myRole, "shadow");
        if shadow ~= nil then
            shadow.transform.localEulerAngles = Vector3.New(40, 180, 0);
        end
        logicMgr.EquipManager.setWeaponSkin(enemyObj, heroID, true);
        Util.PlayAction(enemyObj, "fidle");
    end);
end

function _M:showEnemyInfo()
    self.textPkTips.text = string.format(getLanguage("RandomEventPKTips"), self.data["foePlayerId"].name);

    self:SetPlayerIcon(self.enemyHead:GetChild("head"):GetChild("HeadIcon"), self.data["foePlayerId"].pic, self.enemyHead:GetChild("frame"), self.data["foePlayerId"].picFrame);
    self.enemyName.text = self.data["foePlayerId"].name;
    local unionName = self.data["foePlayerId"].guildName;
    if unionName ~= nil and unionName ~= "" then
        self.enemyGuildName.text = unionName;
    else
        self.enemyGuildName.text = getLanguage("GuildBattlePlusRankTips");
    end
    self.enemyPower.text = HelperFunc.FormatResourceNum(self.data["foePlayerId"].power, true);

    local skinId = self.data["foePlayerId"].pic;
    local heroID = tostring(logicMgr.SkinManager.getHeroModelIdBySkinId(skinId));
    local path = logicMgr.SkinManager.GetHeroModelPathBySkinId(skinId, heroID);
    resMgr:LoadModelPrefab(path, function(go)
        if self.enemyRole ~= nil then
            destroyImmediate(self.enemyRole);
            self.enemyRole = nil
        end
        self.enemyRole = newObject(go);
        self.enemyRole.name = path;
        self.enemyRole:AddComponent(TCanvas)
        if self.enemyWrapper == nil then
            self.enemyWrapper = GoWrapper.New()
        end
        self.enemyWrapper:SetWrapTarget(self.enemyRole,false);
        Util.Lua_SetLocalPositionXYZ(self.enemyRole, 60, -300, 0);
        Util.Lua_SetLocalScaleXYZ(self.enemyRole, 110, 110, 110);
        self.enemyHeroPos:SetNativeObject(self.enemyWrapper);
        local enemyObj = Util.Lua_FindGameObject(self.enemyRole, "Animation");
        enemyObj.transform.localEulerAngles = Vector3.New(0, 180, 0);
        local shadow = Util.Lua_FindGameObject(self.enemyRole, "shadow");
        if shadow ~= nil then
            shadow.transform.localEulerAngles = Vector3.New(40, 180, 0);
        end
        logicMgr.EquipManager.setWeaponSkin(enemyObj, heroID, true);
        Util.PlayAction(enemyObj, "fidle");
    end);
end

function _M:OnConfirm()
    if self.isBuy then
        return;
    end
    if HelperFunc.GetServerTime() > self.data["overdueTime"] then
        UISysTips.AddMsg(getLanguage("ErrorCode_210004"));
        self:OnBack();
    else
        self.isBuy = true;
        -- 点击战斗
        DungeonRandomEventHandler.sendDungeonRandomEventFoeInfo();
    end
end

function _M:OpenFriendCompeteBattle(msg)
    self.isBuy = false;
    if msg == nil then
        self:OnBack();
    else
        local param = {};
        param.friendId = msg.friendId;
        param.supSkillId = msg.supportSkill;
        param.enemyFormation = msg.formation;
        showUI(PanelNames.UIBattleTeam, EnumConst.FightTypeConst.DungeonEventPK, param, function(formations)
            if HelperFunc.GetServerTime() > self.data["overdueTime"] then
                UISysTips.AddMsg(getLanguage("ErrorCode_210004"));
                self:OnBack();
            else
                local flag = true;
                if formations[1] ~= nil then
                    for i = 1, #formations[1] do
                        if formations[1][i] ~= nil and tonumber(tostring(formations[1][i])) > 0 then
                            flag = false;
                            break;
                        end
                    end
                end
                if flag then
                    UISysTips.AddMsg("需要上阵至少一名英雄");
                else
                    self:clearBackFunc();
                    DungeonRandomEventHandler.sendDungeonRandomEventBattle(formations[1]);
                end
            end
        end, function()

        end);
    end
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    if self.myRole ~= nil then
        destroy(self.myRole);
        self.myRole = nil;
    end
    if self.myWrapper then
        self.myWrapper:Dispose();
        self.myWrapper = nil;
    end

    if self.enemyRole ~= nil then
        destroy(self.enemyRole);
        self.enemyRole = nil;
    end
    if self.enemyWrapper then
        self.enemyWrapper:Dispose();
        self.enemyWrapper = nil;
    end
    self.hasInit = false;
end

return _M;